package com.snake.java;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Random;

/**
 * @author wzy
 * @date 2022/3/1 8:39 下午
 */
public class GamePanel extends JPanel implements KeyListener, ActionListener {

    /**
     * 蛇长
     */
    private int length;
    /**
     * 蛇的横坐标
     */
    private int[] snakeX = new int[600];
    /**
     * 蛇的纵坐标
     */
    private int[] snakeY = new int[500];
    /**
     * 蛇头
     */
    String snakeHeader;
    /**
     * 游戏是否开始
     */
    private boolean isStart = false;
    private final Timer timer = new Timer(100, this);
    /**
     * 食物
     */
    private int foodX;
    private int foodY;
    /**
     * 游戏失败
     */
    private boolean isFail = false;
    /**
     * 积分
     */
    private int score;


    public GamePanel() {
        init();
        // 获取键盘监听事件
        this.setFocusable(true);
        this.addKeyListener(this);
        timer.start();
    }

    Random random = new Random();

    private void init() {
        length = 3;
        snakeX[0] = 100;
        snakeY[0] = 100;
        snakeX[1] = 75;
        snakeY[1] = 100;
        snakeX[2] = 50;
        snakeY[2] = 100;
        snakeHeader = "R";
        foodX = 25 + 25 * random.nextInt(34);
        foodY = 75 + 25 * random.nextInt(24);
        score = 0;
    }

    /**
     * 画板：画界线 画蛇
     *
     * @param g 画笔
     */
    @Override
    protected void paintComponent(Graphics g) {
        // 清屏
        super.paintComponent(g);
        // 设置背景颜色
        this.setBackground(Color.WHITE);
        // 绘制头部广告栏
        Data.header.paintIcon(this, g, 25, 11);
        // 绘制游戏区域
        g.fillRect(25, 75, 850, 600);

        // 画🐍
        if ("R".equals(snakeHeader)) {
            Data.right.paintIcon(this, g, snakeX[0], snakeY[0]);
        } else if ("L".equals(snakeHeader)) {
            Data.left.paintIcon(this, g, snakeX[0], snakeY[0]);
        } else if ("U".equals(snakeHeader)) {
            Data.up.paintIcon(this, g, snakeX[0], snakeY[0]);
        } else if ("D".equals(snakeHeader)) {
            Data.down.paintIcon(this, g, snakeX[0], snakeY[0]);
        }

        for (int i = 1; i < length; i++) {
            Data.body.paintIcon(this, g, snakeX[i], snakeY[i]);
        }
        // 食物
        Data.foot.paintIcon(this, g, foodX, foodY);

        // 积分
        g.setColor(Color.WHITE);
        g.setFont(new Font("微软雅黑", Font.BOLD, 18));
        g.drawString("积分：" + score, 750, 50);
        g.drawString("长度：" + length, 750, 35);

        // 游戏提示
        if (!isStart) {
            // 写文字
            g.setColor(Color.WHITE);
            g.setFont(new Font("微软雅黑", Font.BOLD, 40));
            g.drawString("按下空格开始游戏", 300, 300);
        }
        if (isFail) {
            // 写文字
            g.setColor(Color.WHITE);
            g.setFont(new Font("微软雅黑", Font.BOLD, 40));
            g.drawString("游戏失败，按下空格重新开始", 200, 200);
        }
    }

    /**
     * 监听键盘事件
     *
     * @param e 键
     */
    @Override
    public void keyPressed(KeyEvent e) {
        // 键盘按下
        int keyCode = e.getKeyCode();
        if (keyCode == KeyEvent.VK_SPACE) {
            if (isFail) {
                isFail = false;
                init();
            } else {
                isStart = !isStart;
            }


            // 刷新界面
            repaint();
        }
        if (keyCode == KeyEvent.VK_LEFT) {
            snakeHeader = "L";
        } else if (keyCode == KeyEvent.VK_RIGHT) {
            snakeHeader = "R";
        } else if (keyCode == KeyEvent.VK_UP) {
            snakeHeader = "U";
        } else if (keyCode == KeyEvent.VK_DOWN) {
            snakeHeader = "D";
        }

    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (isStart && !isFail) {
            // 身体右移动
            for (int i = length - 1; i > 0; i--) {
                snakeX[i] = snakeX[i - 1];
                snakeY[i] = snakeY[i - 1];
            }
            // 头部
            if ("R".equals(snakeHeader)) {
                snakeX[0] = snakeX[0] + 25;
                if (snakeX[0] > 850) {
                    snakeX[0] = 25;
                }
            } else if ("L".equals(snakeHeader)) {
                snakeX[0] = snakeX[0] - 25;
                if (snakeX[0] < 25) {
                    snakeX[0] = 850;
                }
            } else if ("U".equals(snakeHeader)) {
                snakeY[0] = snakeY[0] - 25;
                if (snakeY[0] < 75) {
                    snakeY[0] = 650;
                }
            } else if ("D".equals(snakeHeader)) {
                snakeY[0] = snakeY[0] + 25;
                if (snakeY[0] > 650) {
                    snakeY[0] = 75;
                }
            }
            // 吃食物
            if (snakeX[0] == foodX && snakeY[0] == foodY) {
                length++;
                score = score + 10;
                foodX = 25 + 25 * random.nextInt(34);
                foodY = 75 + 25 * random.nextInt(24);
            }
            // 碰到自己失败
            for (int i = 1; i < length; i++) {
                if (snakeX[i] == snakeX[0] && snakeY[i] == snakeY[0]) {
                    isFail = true;
                    init();
                }
            }

            // 刷新页面
            repaint();
        }
        timer.start();
    }


    @Override
    public void keyTyped(KeyEvent e) {
        // 键盘按下弹起
    }

    @Override
    public void keyReleased(KeyEvent e) {
        // 键盘弹起
    }
}
